/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sellcar;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fabio Valenzuela
 */
public class Tela_PesqExc_Usuario extends javax.swing.JFrame {
       private Connection connection;
     

    /** Creates new form Tela_PesqExc_Usuario */
    public Tela_PesqExc_Usuario() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.connection = new ConnectionFactory().getConnection();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BtPesquisa = new javax.swing.JButton();
        BtVoltar = new javax.swing.JButton();
        TxtSenha = new javax.swing.JPasswordField();
        Senha = new javax.swing.JLabel();
        TxtUsuario = new javax.swing.JTextField();
        Usuario = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        TxtCodCad = new javax.swing.JTextField();
        CodCadastro = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        BtAnterior = new javax.swing.JButton();
        BtProximo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        BtPesquisa.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        BtPesquisa.setText("Pesquisar");
        BtPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtPesquisaActionPerformed(evt);
            }
        });

        BtVoltar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        BtVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sellcar/Icones/arrow_left.png"))); // NOI18N
        BtVoltar.setMnemonic('S');
        BtVoltar.setText("Voltar");
        BtVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtVoltarActionPerformed(evt);
            }
        });

        TxtSenha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        Senha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        Senha.setText("Senha");

        TxtUsuario.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        TxtUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TxtUsuarioActionPerformed(evt);
            }
        });

        Usuario.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        Usuario.setText("Usuário");

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sellcar/Icones/cancel.png"))); // NOI18N
        jButton2.setText("Excluir");

        TxtCodCad.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        CodCadastro.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        CodCadastro.setText("Cod Cadastro");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton1.setText("Atualizar");

        BtAnterior.setText("anterior");
        BtAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtAnteriorActionPerformed(evt);
            }
        });

        BtProximo.setText("proximo");
        BtProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtProximoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(TxtSenha)
                                    .addComponent(TxtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                                    .addComponent(TxtCodCad))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(Usuario)
                                    .addComponent(Senha)
                                    .addComponent(CodCadastro))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 24, Short.MAX_VALUE)
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BtPesquisa)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BtVoltar))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(161, 161, 161)
                        .addComponent(BtAnterior)
                        .addGap(27, 27, 27)
                        .addComponent(BtProximo)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TxtCodCad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CodCadastro))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TxtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Usuario))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Senha)
                    .addComponent(TxtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtAnterior)
                    .addComponent(BtProximo))
                .addGap(92, 92, 92)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BtVoltar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtPesquisa)
                        .addComponent(jButton2)
                        .addComponent(jButton1)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtVoltarActionPerformed
        // Botão Voltar
        // Encerra a tela de cadastro
        this.dispose();
    }//GEN-LAST:event_BtVoltarActionPerformed

    private void TxtUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TxtUsuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TxtUsuarioActionPerformed

    private void BtPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtPesquisaActionPerformed
        // TODO add your handling code here:
        try {             
            ResultSet rs;
            String sql = "SELECT id_usuario,desc_login,senha FROM tb_usuario";
            PreparedStatement stmt = connection.prepareStatement(sql);
            rs = stmt.executeQuery(sql);
            
            DefaultTableModel modeloTabela = null;
            

if (rs.first()) {
List lista = new ArrayList(); 
for (int i = 0; i < lista.size(); ++i) { 
Tela_PesqExc_Usuario obj = (Tela_PesqExc_Usuario) lista.get(i); 


            modeloTabela.addColumn("id_usuario");
            modeloTabela.addColumn("desc_login");
            modeloTabela.addColumn("senha"); 
            modeloTabela = new DefaultTableModel();
        TxtCodCad.setText(rs.getString("id_usuario"));
        TxtUsuario.setText(rs.getString("desc_login"));
        TxtSenha.setText(rs.getString("senha"));
}
}
        } catch (SQLException ex) {
            Logger.getLogger(Cadastro_Usuario.class.getName()).log(Level.SEVERE, null, ex);
    } 
    }//GEN-LAST:event_BtPesquisaActionPerformed

    private void BtProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtProximoActionPerformed
        // TODO add your handling code here:
        try {             
            ResultSet rs;
            String sql = "SELECT id_usuario,desc_login,senha FROM tb_usuario";
            PreparedStatement stmt = connection.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
            
                        
    //DefaultTableModel modeloTabela = null;
    while(rs.next()){
        List lista = new ArrayList(); 
for (int i = 0; i < lista.size(); ++i) { 
Tela_PesqExc_Usuario obj = (Tela_PesqExc_Usuario) lista.get(i);

//     modeloTabela = new DefaultTableModel();
//
//     modeloTabela.addColumn("id_usuario");
//     modeloTabela.addColumn("desc_login");
//     modeloTabela.addColumn("senha");
        
        TxtCodCad.setText(rs.getString("id_usuario"));
        TxtUsuario.setText(rs.getString("desc_login"));
        TxtSenha.setText(rs.getString("senha"));

} }       
  } catch (SQLException ex) {
            Logger.getLogger(Tela_PesqExc_Usuario.class.getName()).log(Level.SEVERE, null, ex);
    } 
        
    }//GEN-LAST:event_BtProximoActionPerformed

    private void BtAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtAnteriorActionPerformed
        // TODO add your handling code here:
        try {             
            ResultSet rs = null;
            String sql = "SELECT id_usuario,desc_login,senha FROM tb_usuario";
            PreparedStatement stmt = connection.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
            
                        
    DefaultTableModel modeloTabela = null;
    while(rs.previous()){

     modeloTabela = new DefaultTableModel();

     modeloTabela.addColumn("id_usuario");
     modeloTabela.addColumn("desc_login");
     modeloTabela.addColumn("senha");
        
        TxtCodCad.setText(rs.getString("id_usuario"));
        TxtUsuario.setText(rs.getString("desc_login"));
        TxtSenha.setText(rs.getString("senha"));

}        
  } catch (SQLException ex) {
            Logger.getLogger(Tela_PesqExc_Usuario.class.getName()).log(Level.SEVERE, null, ex);
    } 
    }//GEN-LAST:event_BtAnteriorActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Tela_PesqExc_Usuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Tela_PesqExc_Usuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Tela_PesqExc_Usuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Tela_PesqExc_Usuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tela_PesqExc_Usuario().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtAnterior;
    private javax.swing.JButton BtPesquisa;
    private javax.swing.JButton BtProximo;
    private javax.swing.JButton BtVoltar;
    private javax.swing.JLabel CodCadastro;
    private javax.swing.JLabel Senha;
    private javax.swing.JTextField TxtCodCad;
    private javax.swing.JPasswordField TxtSenha;
    private javax.swing.JTextField TxtUsuario;
    private javax.swing.JLabel Usuario;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables
    
}